package org.cwgl.actions;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.cwgl.interfaces.BaseDataService;
import org.cwgl.util.Json;
import org.cwgl.vo.Result;
import org.cwgl.vo.UsercaseVo;

public class UsercaseAction {

	// 前台封装的案例对象
	public UsercaseVo usercase = new UsercaseVo();

	// 基础数据业务类
	public BaseDataService service;

	public BaseDataService getService() {
		return service;
	}

	public void setService(BaseDataService service) {
		this.service = service;
	}

	public UsercaseVo getUsercase() {
		return usercase;
	}

	public void setUsercase(UsercaseVo usercase) {
		this.usercase = usercase;
	}

	/**
	 * 添加案例
	 * 
	 * @return
	 */
	public String addUsercase() {
		Result result = new Result();
		result.setSuccess(false);
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = null;
		try 
		{
			out = response.getWriter();
			result.setSuccess(true);
		} catch (IOException e) 
		{
			result.setException(e.getMessage());
		} finally 
		{
			if (result.getSuccess() == true) {
				result = service.addUsercase(this.usercase);
			}
			out.print(Json.JSON_Object(result));
			out.flush();
		}
		return (null);
	}

	/**
	 * 更新案例
	 * 
	 * @return
	 */
	public String updateUsercase() {
		Result result = new Result();
		result.setSuccess(false);
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = null;
		try 
		{
			out = response.getWriter();
			result.setSuccess(true);
		} catch (IOException e) 
		{
			result.setException(e.getMessage());
		} finally 
		{
			if (result.getSuccess() == true) {
				result = service.updateUsercase(this.usercase);
			}
			out.print(Json.JSON_Object(result));
			out.flush();
		}
		return (null);
	}

	/**
	 * 删除案例
	 * 
	 * @return
	 */
	public String delUsercase() {
		Result result = new Result();
		result.setSuccess(false);
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = null;
		try 
		{
			out = response.getWriter();
			result.setSuccess(true);
		} catch (IOException e) 
		{
			result.setException(e.getMessage());
		} finally 
		{
			if (result.getSuccess() == true) {
				result = service.deleteUsercase(this.usercase);
			}
			out.print(Json.JSON_Object(result));
			out.flush();
		}
		return (null);
	}
}
